<script setup>
import { ref, onMounted, computed, provide } from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'
import Info from '../pojo/Info.js'
import Member from '../pojo/Member.js'
import Photo from '../pojo/Photo.js'
import { getAllInfoApi } from '../apis/infoApi.js'
import { getMemberListApi } from '../apis/memberApi.js'
import { getPhotoListApi } from '../apis/photoApi.js'

// 三类数据
const infoData = ref([])
const memberData = ref([])
const photoData = ref([])
const loading = ref(true)

// 按 title 分组的基本信息数据
const infoByTitle = computed(() => {
  const grouped = {}
  infoData.value.forEach(info => {
    if (!grouped[info.title]) {
      grouped[info.title] = []
    }
    grouped[info.title].push(info)
  })
  return grouped
})

// 获取本人格言
const personalMotto = computed(() => {
  const mottoInfo = infoData.value.find(info => info.title === '本人格言')
  return mottoInfo ? mottoInfo.description : ''
})

// 获取个人简介图片
const personalPhoto = computed(() => {
  const photo = photoData.value.find(photo => photo.photoName === '个人简介')
  return photo ? photo.content : 'src/assets/images/TCavatar.png'
})

// 获取基本信息数据
const fetchInfoData = async () => {
  try {
    const response = await getAllInfoApi()
    if (response.data.code === 200 && response.data.data) {
      console.log("基本信息数据") // Add this line to log the response data to the console
  
      infoData.value = response.data.data.map(item => new Info(
        item.id,
        item.title,
        item.description,
        item.displayOrder
      ))
      console.log(infoData.value)
    } else {
      ElMessage.warning('获取基本信息失败：' + (response.data.msg || '未知错误'))
      infoData.value = []
    }
  } catch (error) {
    console.error('获取基本信息出错:', error)
    ElMessage.error('获取基本信息出错')
    infoData.value = []
  }
}

// 获取成员数据
const fetchMemberData = async () => {
  try {
    const response = await getMemberListApi()
    if (response.data.code === 200 && response.data.data) {
      memberData.value = response.data.data.map(item => new Member(
        item.id,
        item.name,
        item.classify,
        item.grade,
        item.avatar,
        item.researchInterest,
        item.currentStatus,
        item.phone,
        item.email,
        item.thesis,
        item.project,
        item.contest,
        item.reward
      ))
    } else {
      ElMessage.warning('获取成员数据失败：' + (response.data.msg || '未知错误'))
      memberData.value = []
    }
  } catch (error) {
    console.error('获取成员数据出错:', error)
    ElMessage.error('获取成员数据出错')
    memberData.value = []
  }
}

// 获取图片数据
const fetchPhotoData = async () => {
  try {
    const response = await getPhotoListApi()
    if (response.data.code === 200 && response.data.data) {
      photoData.value = response.data.data.map(item => new Photo(
        item.id,
        item.photoName,
        item.photoAlt,
        item.content
      ))
    } else {
      ElMessage.warning('获取图片数据失败：' + (response.data.msg || '未知错误'))
      photoData.value = []
    }
  } catch (error) {
    console.error('获取图片数据出错:', error)
    ElMessage.error('获取图片数据出错')
    photoData.value = []
  }
}

// 获取所有数据
const fetchAllData = async () => {
  try {
    loading.value = true
    await Promise.all([
      fetchInfoData(),
      fetchMemberData(),
      fetchPhotoData()
    ])
  } finally {
    loading.value = false
  }
}

// 提供数据给子组件
provide('infoData', infoData)
provide('memberData', memberData)
provide('photoData', photoData)
provide('infoByTitle', infoByTitle)
provide('loading', loading)

onMounted(() => {
  fetchAllData()
  console.log(infoData.value)
  console.log(memberData.value)
  console.log(photoData.value)
})
</script>

<template>
  <div class="box">
    <div style="background-color: white;text-align: center">
      <h1 style="font-size: large;font-weight: bold">唐超 副教授(博士)</h1>
    </div>
    <div class="brief">
      <div class="description">
        <h1>职称：副教授</h1>
        <h1>职务：教师</h1>
        <h1>工作单位：合肥学院 人工智能与大数据学院</h1>
        <h1>E-MAIL：tangchao@hfuu.edu.cn</h1>
      </div>
      <div class="photo">
        <el-image 
            style="width: 150px; height: 150px" 
            :src="personalPhoto" 
            fit="cover"></el-image>
      </div>
    </div>

    <!-- 本人格言 -->
    <div v-if="personalMotto" class="motto-section">
      <div class="motto-content">
        <i class="motto-icon">💭</i>
        <p class="motto-text">{{ personalMotto }}</p>
      </div>
    </div>

    <div class="nav">
      <ul class="nav-list">
        <li>
          <RouterLink to="/" class="nav-link">
            基本情况 & 主要研究方向
          </RouterLink>
        </li>
        <li>
          <RouterLink to="/research" class="nav-link">
            主要科研成果
          </RouterLink>
        </li>
        <li>
          <RouterLink to="/teaching" class="nav-link">
            主要教学成果
          </RouterLink>
        </li>
        <li>
          <RouterLink to="/admission" class="nav-link">
            研究生招生说明
          </RouterLink>
        </li>
        <li>
          <RouterLink to="/contact" class="nav-link">
            联系方式
          </RouterLink>
        </li>
        <li>
          <RouterLink to="/team" class="nav-link">
            研究小组展示
          </RouterLink>
        </li>
      </ul>
    </div>

    <div class="content">
      <RouterView/>
    </div>
  </div>
</template>

<style scoped>
.box {
  min-height: 80vh;
  width: 75%;
  height: 100%;
  margin: 20px auto 0;

}

.brief {
  width: 100%;
  height: 200px;
  background-color: white;
  display: flex;
  align-items: center; /* 垂直居中 */
  justify-content: space-between; /* 文字在左边，图片在右边 */
}

.description {
  display: flex;
  flex-direction: column; /* 文字垂直排列 */
  justify-content: space-between; /* 文字在盒子内均匀分布 */
  height: 100%; /* 占满整个父盒子高度 */
  padding: 0 20px; /* 添加一些内边距 */
}

.photo {
  margin-right: 20px; /* 添加一些右边距 */
}

.motto-section {
  width: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
  margin: 20px 0;
  border-radius: 10px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.motto-content {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 15px;
}

.motto-icon {
  font-size: 2rem;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.2));
}

.motto-text {
  color: white;
  font-size: 1.2rem;
  font-style: italic;
  font-weight: 500;
  text-align: center;
  line-height: 1.6;
  margin: 0;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  max-width: 80%;
}

.nav {
  width: 100%;
  height: 50px;
  background-color: #f5f5f5;
  padding: 0 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.nav-list {
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
  height: 50px;
  align-items: center;
  overflow-x: auto; /* 允许水平滚动 */
}

.nav-list li {
  flex-shrink: 0; /* 防止链接被压缩 */
  margin-right: 30px;
}

.nav-link {
  text-decoration: none;
  color: #333;
  font-weight: 500;
  padding: 8px 12px;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.nav-link:hover {
  background-color: #e0e0e0;
  color: #2196F3;
}

.router-link-active {
  color: #2196F3;
  background-color: #e3f2fd;
  border-bottom: 2px solid #2196F3;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .motto-section {
    padding: 15px;
    margin: 15px 0;
  }
  
  .motto-content {
    flex-direction: column;
    gap: 10px;
  }
  
  .motto-text {
    font-size: 1.1rem;
    max-width: 95%;
  }
  
  .nav-list {
    padding: 0 15px;
  }

  .nav-list li {
    margin-right: 20px;
  }

  .nav-link {
    font-size: 14px;
    padding: 6px 10px;
  }
}

@media (max-width: 480px) {
  .motto-section {
    padding: 12px;
  }
  
  .motto-text {
    font-size: 1rem;
  }
  
  .motto-icon {
    font-size: 1.5rem;
  }
}
</style>
